<template>
	<view class="my-box" v-if="sta!==null">
		<noLogin @handLogin='handLogin' v-if='sta==1'></noLogin>
		<noPhone @handPhone='handPhone' v-if='sta==2'></noPhone>
		<noMsg @handMsg='handMsg' v-if='sta==3'></noMsg>
		<myConter :userMsg='userMsg' v-if='sta==4'></myConter>
	</view>
</template>

<script>
	import noLogin from './components/no-login.vue'
	import noPhone from './components/no-phone.vue'
	import noMsg from './components/no-msg.vue'
	import myConter from './components/my-conter.vue'
	export default {
		components: {
			noLogin,
			noPhone,
			noMsg,
			myConter
		},
		data() {
			return {
				sta: null,
				userCode: '',
				userMsg: {}
			};
		},
		onShow() {
			// this.cache.getCache('phone')
			const userCode = this.cache.getCache('userCode')
			const phone = this.cache.getCache('phone')
			this.userMsg = this.cache.getCache('userMsg')
			const userType = this.cache.getCache('userMsg').userType
			uni.setNavigationBarTitle({
				title: this.sta == 4 ? '个人中心' : '登录'
			})
			if (!userCode) {
				this.sta = 1
				return
			}
			if (!phone) {
				this.sta = 2
				return
			}
			if (userType === undefined || userType === null) {
				this.sta = 3
				return
			}
			this.sta = 4
			console.log(this.userMsg, 'token==>', !userCode, '===手机号==》', userType)

		},
		methods: {
			handMsg() {
				uni.navigateTo({
					url: '/pages/my/perfectMsg/perfectMsg'
				})
			},
			//登录获取用户信息
			handLogin() {
				let _this = this
				uni.login({
					success: function(res) {
						_this.$u.api.login({
							code: res.code
						}).then(resInfo => {
							//userCode就是token
							_this.userCode = resInfo.userCode
							console.log(resInfo, '用户信息', _this.userCode)
							if (resInfo && resInfo.phone) {
								_this.cache.setCache('userCode', resInfo.userCode)
								_this.cache.setCache('userMsg', resInfo)
								_this.cache.setCache('phone', resInfo.phone)
								uni.switchTab({
									url: '/pages/my/my'
								})
							} else {
								uni.showToast({
									title: '请授权手机号以获得完整服务',
									icon: 'none'
								})
								_this.sta = 2
							}

						})
					}
				})
			},
			//获取用户手机号
			async handPhone(e) {
				const encryptedData = e.detail.encryptedData;
				const ivStr = e.detail.iv;
				let _this = this
				_this.cache.setCache('userCode', this.userCode)
				if (!encryptedData || !ivStr) {
					_this.cache.removeCache('userCode')
					_this.cache.removeCache('phone')
					uni.showToast({
						title: '请授权手机号以获得完整服务~',
						icon: 'none',
					})
					return
				};
				// uni.navigateTo({
				// 	url: '/pages/my/perfectMsg/perfectMsg'
				// })
				console.log(this.userCode)
				let res = await this.$u.api.getPhone({
					encryptedData: encryptedData,
					iv: ivStr
				}); //设置手机号
				console.log(res, '手机号')
				if (res.phoneNumber) {
					this.cache.setCache('phone', res.phoneNumber)
					this.sta = 3
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-box {}
</style>
